﻿using Mongodb;
using ProductTool.Entity;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq.Expressions;
using System.Linq;
using ProductTool.Dao;
using ProductTool.Tools;
using System.Threading.Tasks;
using MassTransit.Util;

namespace ProductTool.Service
{
    public class CommentService
    {
     
        ItemDao dao = new ItemDao();
        ItemService itemService = new ItemService();
        public void Process() {

            var list = new List<ItemEntity>();
            int page = 1;
            int pageSize = 100;
            do
            {
                list = dao.QueryItemList(page, pageSize);
                while (!MakeComment(list))
                {
                    Console.WriteLine("...........service err,retrying......");
                    MakeComment(list);
                }
                Console.WriteLine($"{page * pageSize} success");
                Console.WriteLine($".................page {page} complete.........");
                page++;
            } while (list.Count > 0);

            Console.WriteLine("-----------------------all done----------------------------------");
            Console.ReadKey();
            var itemList = dao.QueryItemList(1,10);         
        }

        private bool MakeComment(List<ItemEntity> list)
        {
            var snList = list.Select(p => p.goods_sn).ToList();
            MongoContext mc = new MongoContext();
            var db = mc.DbSet<Comment>("items_comment");
           // var data = db.FindToList(p => true).ToList();
            var data = db.FindToList(p => snList.Contains(p.good_ID) );
            TaskFactory fac = new TaskFactory(new LimitedConcurrencyLevelTaskScheduler(10));
            List<Task> taskList = new List<Task>();
            foreach (var item in data)
            {                
                taskList.Add(fac.StartNew(() =>
                {
                    var entity = new YComment
                    {
                        content = item.content,
                        rating = item.score,
                        ip = "127.0.0.1"
                    };
                    var item_number = list.First(p => p.goods_sn == item.good_ID)?.item_number;
                    if (item_number != null)
                    {
                        var result = ServiceHelper.CallService<ResponseBase>($"http://ecapi.ecnow.tech/ec-sns/products/{item_number}/comments", entity, Common.ec_token);
                        if (result != null && result.messageId != 10000 || !result.success)
                        {
                            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(result));
                        }
                        else
                        {
                            Console.WriteLine($"{item_number}-{item.user_ID}---评论成功.");
                        }
                    }                 

                })
                );

            }
            Task.WaitAll(taskList.ToArray());
            return true;
        }

        /// <summary>
        /// 根据评论导商品
        /// </summary>
        public void Process2() {
            MongoContext mc = new MongoContext();
            var db = mc.DbSet<Comment>("items_comment2");
            var data = db.FindToList(p => true).ToList().Skip(100).Take(100);
            foreach (var item in data)
            {
                var im = dao.CheckItemExists(item.good_ID);
                //if (im == null)
                //{
                //    var goods = dao.QueryJdItemBySku(item.good_ID);
                //    if (goods!=null)
                //    {
                //        var res=itemService.createItem(goods);
                //        im = dao.CheckItemExists(item.good_ID);
                //    }
                //}
                if (im!=null)
                {
                    MakeComment2(item, im.goods_id.ToString());
                    Console.WriteLine($"{im.item_number} success");
                }               
            }
            //http://alicentralapi.yamibuy.net/sns/posts/productExt/cache

        }
        private bool MakeComment2(Comment comment,string item_number)
        {
            var entity = new YComment
            {
                content = comment.content,
                rating = comment.score,
                ip = "127.0.0.1",
                imageList=new ImageInfo[0]
            };
            var result = ServiceHelper.CallService<ResponseBase>($"http://ecapi.ecnow.tech/ec-sns/products/{item_number}/comments", entity, Common.token);
            if (result != null && result.messageId != 10000 || !result.success)
            {
                Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }           
            return true;
        }
    }
}
